package android.FiltrosJava;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class SineDeformation extends AbstractImageBasic{
	protected int amplitude, period;
		
	public SineDeformation(int sinAmplitude, int sinPeriod){
		setSinDeformation(sinAmplitude, sinPeriod);
	}
	
	public void setSinDeformation(int sinAmplitude, int sinPeriod){
		amplitude = sinAmplitude;
		period = sinPeriod;			
	}
	
	public BufferedImage applyFilter(BufferedImage dst) {
		BufferedImage src = copiaImagem(dst);
		
		double freq = (float) (1.0f / period);
		int x2, y2;
		int dimX = src.getWidth();
		int dimY = src.getHeight();
		WritableRaster raster_src = src.getRaster();
    	WritableRaster raster_dst = dst.getRaster();    	
    	int[] imageData = new int[raster_src.getNumBands()]; 
		
		for (int x = 0; x < dimX; x++){
			for (int y = 0; y < dimY; y++){
				x2 = (int) (x + ( amplitude * Math.sin(y * freq)));
	         	y2 = (int) (y + ( amplitude * Math.cos(x * freq)));
	         		         	
	         	if ((x2 < 0) || (x2 >= dimX) || (y2 < 0) || (y2 >= dimY)){
	         		imageData[0] = 0;
		         	imageData[1] = 0;
		         	imageData[2] = 0;
		         	raster_dst.setPixel(x,y,imageData);
	         	}else{	         		   			
	         		raster_src.getPixel(x2,y2,imageData);	         		
		    		raster_dst.setPixel(x,y,imageData);	         		
	         	}	         						
			}
		}
		src = null;
		return dst;		
    }

}